package com.linxianqin.springbootVue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.convert.SysLogininforConvert;
import com.linxianqin.springbootVue.domain.convert.SysOperLogConvert;
import com.linxianqin.springbootVue.domain.dto.SysOperLogDTO;
import com.linxianqin.springbootVue.domain.entity.SysLogininfor;
import com.linxianqin.springbootVue.domain.entity.SysOperLog;
import com.linxianqin.springbootVue.domain.query.operLogQuery;
import com.linxianqin.springbootVue.mapper.SysOperLogMapper;
import com.linxianqin.springbootVue.service.SysOperLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linxianqin.springbootVue.utils.DateUtils;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author lxq
 * @since 2020-12-13
 */
@Slf4j
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {
    /**
     * 新增操作日志
     *
     * @param operLogDTO 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLogDTO operLogDTO) {
        baseMapper.insert(SysOperLogConvert.INSTANCE.dto2entity(operLogDTO));
    }
    /**
     * 查询系统操作日志集合
     *
     * @param query 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public Page<SysOperLogDTO> selectOperLogPage(Page<SysOperLogDTO> page, operLogQuery query) {
        Page<SysOperLog> entityPage = SysOperLogConvert.INSTANCE.dto2entity(page);
        LambdaQueryWrapper<SysOperLog> queryWrapper=new QueryWrapper<SysOperLog>().lambda();
        if (StringUtils.isNotNull(query.getTitle())){
            queryWrapper.like(SysOperLog::getTitle,query.getTitle());
        }
        if (StringUtils.isNotNull(query.getOperName())){
            queryWrapper.like(SysOperLog::getOperName,query.getOperName());
        }
        if (StringUtils.isNotNull(query.getBusinessType())){
            queryWrapper.eq(SysOperLog::getBusinessType,query.getBusinessType());
        }
        if (StringUtils.isNotNull(query.getStatus())){
            queryWrapper.eq(SysOperLog::getStatus,query.getStatus());
        }
        if (StringUtils.isNotNull(query.getParams())){
            Map<String, Object> params = query.getParams();
            Object beginTime = params.get("beginTime");
            if (beginTime!=null){
                queryWrapper.ge(SysOperLog::getOperTime,beginTime);
            }
            Object endTime = params.get("endTime");
            if (beginTime!=null){
                endTime = DateUtils.addOneDay(DateUtils.dateTime("yyyy-MM-dd", endTime.toString()));
                queryWrapper.le(SysOperLog::getOperTime,endTime);
            };
        }
        Page<SysOperLog> selectPage = baseMapper.selectPage(entityPage, queryWrapper);
        return SysOperLogConvert.INSTANCE.entity2dto(selectPage);
    }
    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        baseMapper.cleanOperLog();
    }
}
